package com.zxh.blog

import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import androidx.fragment.app.Fragment
import com.google.android.material.bottomnavigation.BottomNavigationView
import com.gyf.immersionbar.ImmersionBar
import com.zxh.blog.fragment.CategoryFragment
import com.zxh.blog.fragment.HomeFragment
import com.zxh.blog.fragment.LinkFragment
import com.zxh.blog.fragment.MineFragment

class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        ImmersionBar.with(this)
            .statusBarColor(R.color.app_back)
            .statusBarDarkFont(true)
            .titleBar(R.id.fragmentContainer)
            .init()
        // 设置底部按钮点击事件
        val bottomNavigationView = findViewById<BottomNavigationView>(R.id.bottomNavigationView)
        bottomNavigationView.isItemActiveIndicatorEnabled = false
        bottomNavigationView.setOnItemSelectedListener {
            when (it.itemId) {
                // 跳转到首页
                R.id.action_home -> {
                    replaceFragment(HomeFragment())
                    true
                }

                // 跳转到分类页面
                R.id.action_category -> {
                    replaceFragment(CategoryFragment())
                    true
                }

                // 跳转到友链页面
                R.id.action_link -> {
                    replaceFragment(LinkFragment())
                    true
                }

                // 跳转到我的页面
                R.id.action_profile -> {
                    replaceFragment(MineFragment())
                    true
                }

                else -> false
            }
        }
        // 默认显示首页
        replaceFragment(HomeFragment())
    }

    private fun replaceFragment(fragment: Fragment) {
        supportFragmentManager.beginTransaction()
            .replace(R.id.fragmentContainer, fragment)
            .commit()
    }
}